package com.fjrz.bbxwj.main.video.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.VideoList
import com.fjrz.bbxwj.main.video.CustomGSYVideoPlayer

class PlayerGSYAdapter :
    BaseQuickAdapter<VideoList, BaseViewHolder>(R.layout.item_test_video) {

    companion object {
        private const val TAG = "PlayerGSYAdapter"
    }

    init {
        addChildClickViewIds(
            R.id.ivControlsHeads,
            R.id.ivControlsAttention,
            R.id.tvControlsApprove,
            R.id.tvControlsDiscuss,
            R.id.tvControlsShare,
            R.id.tvControlsAdDetails
        )
    }

    var mSelectPosition = 0

    override fun convert(holder: BaseViewHolder, item: VideoList) {

        val gsyVideoPlayer: CustomGSYVideoPlayer = holder.getView(R.id.gsyVideoPlayer)
        val gsyVideoOption: GSYVideoOptionBuilder = GSYVideoOptionBuilder()
            .setUrl(item.path)
            .setCacheWithPlay(true)
            .setLooping(true)
            .setPlayTag(holder.layoutPosition.toString())
            .setPlayPosition(holder.layoutPosition)
            .setReleaseWhenLossAudio(false)
            .setStartAfterPrepared(true)
        gsyVideoPlayer.setVideoList(item)

        gsyVideoOption.build(gsyVideoPlayer)

        if (holder.layoutPosition == 0 || mSelectPosition == holder.layoutPosition) {
            gsyVideoPlayer.startPlayLogic()
        }

    }

    fun getCustomGSYVideoPlayer(position: Int): CustomGSYVideoPlayer? =
        recyclerView.findViewHolderForLayoutPosition(position)?.itemView?.findViewById(R.id.gsyVideoPlayer)
}